package com.example.springmvcdemo4;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/session")
public class SessionController {
    @RequestMapping("/getC")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            Arrays.stream(cookies).forEach((ck->{
                System.out.println(ck.getName()+":"+ck.getValue());
            }));
        }
        return "获取Cookie成功";
    }

    @RequestMapping("/getC2")
    public String getCookie2(@CookieValue("xmy")String s){
        return s;
    }
    //设置Session
    @RequestMapping("/setSess")
    public String setSess(HttpSession session){
        session.setAttribute("name","zhangsan");
        return "设置session成功";
    }
    //获取Session
    @RequestMapping("/getSess")
    public String getSess(@SessionAttribute("name")String name){
        return  name;
    }
    //获取Header
    @RequestMapping("/getH")
    public String getHeader(@RequestHeader("User-Agent")String userAgent){
        return "User-Agent:"+userAgent;
    }
}
